package com.css.crm.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import com.css.crm.cache.CrmCache;
import com.css.crm.service.CustomerService;
import com.css.crm.service.SolrService;
import com.css.crm.service.SystemService;
import com.css.crm.service.impl.CustomerServiceImpl;
import com.css.crm.service.impl.SolrServiceImpl;
import com.css.crm.service.impl.SystemServiceImpl;
import com.css.crm.utility.ConstantUtil;
import com.css.crm.utility.SelectItemsUtil;
import com.css.crm.vo.AbstractObjectPermission;
import com.css.crm.vo.CustomerAddressVo;
import com.css.crm.vo.CustomerAttributeVo;
import com.css.crm.vo.CustomerDiscountVo;
import com.css.crm.vo.CustomerEmailVo;
import com.css.crm.vo.CustomerServiceEventVo;
import com.css.crm.vo.CustomerTelephoneVo;
import com.css.crm.vo.CustomerVo;
import com.css.crm.vo.MenuItem;
import com.css.crm.vo.MenuMap;
import com.css.crm.vo.PermissionCollection;

public class CustomerBean extends CommonBean {

	public List<MenuItem> accessPath;

	protected String activeTab = "customerTabInfoId";

	protected CustomerVo customer;
	protected String customerName;

	private SelectItem dobMonthItems[];
	protected SelectItem dobDayItems[];
	private SelectItem frChanlItems[];
	private SelectItem attrCodeItems[];
	private SelectItem discountItems[];
	private SelectItem serviceItems[];
	private SelectItem saluteItems[];
	private SelectItem departmentItems[];
	private SelectItem customerStatusItems[];

	protected CustomerService customerService = new CustomerServiceImpl();
	protected SolrService solrService = new SolrServiceImpl();
	protected SystemService systemService = new SystemServiceImpl();

	private String functionId = null;
	List<AbstractObjectPermission> functionList = null;

	public CustomerBean() {
		super();

		dobMonthItems = SelectItemsUtil.getMonthItems();
		dobDayItems = SelectItemsUtil.getDayItems(1);
		saluteItems = CrmCache.getTitleSelectItems();
		frChanlItems = CrmCache.getFrChanlSelectItems();
		attrCodeItems = CrmCache.getArributeCodeSelectItems();
		discountItems = CrmCache.getDiscountCodeISelectItems();
		serviceItems = CrmCache.getServiceCodeISelectItems();
		departmentItems = CrmCache.getDepartmentSelectItems();
		customerStatusItems = SelectItemsUtil.getCustomerStatusItems();

		accessPath = MenuMap.menuMapInstance.getCurrentAccessPath(getFunctionId());

	}

	protected String getFunctionId() {
		if (functionId == null) {
			functionId = getRequestParameter(ConstantUtil.PARAM_FUNC_ID).toString();
		}
		return functionId;
	}

	public List<AbstractObjectPermission> getFunctionList() {
		if (functionList != null) {
			return functionList;
		}

		FacesContext fc = FacesContext.getCurrentInstance();
		PermissionCollection pc = (PermissionCollection) fc.getApplication().getELResolver().getValue(fc.getELContext(), null, "permissionCollection");

		List<AbstractObjectPermission> pl = pc.getPermissionList();
		functionList = new ArrayList<>();
		for (AbstractObjectPermission p : pl) {
			if (ConstantUtil.MENU_FUNC.equalsIgnoreCase(p.getType()) && (p.isReadable() || p.isWritable())) {
				if (p.getParentObjectId().equals(getFunctionId())) {
					functionList.add(p);
				}
			}
		}
		return functionList;
	}

	/**
	 * add telephone record
	 * 
	 * @param event
	 */
	public void addTelephone(ActionEvent event) {
		List<CustomerTelephoneVo> customerTelephoneList = customer.getCustomerTelephoneList();
		if (customerTelephoneList == null) {
			customerTelephoneList = new ArrayList<CustomerTelephoneVo>();
		}
		customerTelephoneList.add(new CustomerTelephoneVo());
		customer.setCustomerTelephoneList(customerTelephoneList);
	}

	/**
	 * delete a telephone record
	 * 
	 * @param event
	 */
	public void deleteTelephone(ActionEvent event) {
		String index = (String) getRequestParameter("index");
		if (index != null && index.length() > 0) {
			if (customer != null && customer.getCustomerTelephoneList() != null && customer.getCustomerTelephoneList().size() > 0) {
				if (customer.getCustomerTelephoneListDeleted() == null) {
					customer.setCustomerTelephoneListDeleted(new ArrayList<CustomerTelephoneVo>());
				}
				customer.getCustomerTelephoneListDeleted().add(customer.getCustomerTelephoneList().get(Integer.parseInt(index)));
				customer.getCustomerTelephoneList().remove(Integer.parseInt(index));
			}
		}
	}

	/**
	 * add email record
	 * 
	 * @param event
	 */
	public void addEmail(ActionEvent event) {
		List<CustomerEmailVo> customerEmailList = customer.getCustomerEmailList();
		if (customerEmailList == null) {
			customerEmailList = new ArrayList<CustomerEmailVo>();
		}
		customerEmailList.add(new CustomerEmailVo());
		customer.setCustomerEmailList(customerEmailList);
	}

	/**
	 * delete email record
	 * 
	 * @param event
	 */
	public void deleteEmail(ActionEvent event) {
		String index = (String) getRequestParameter("index");
		if (index != null && index.length() > 0) {
			if (customer != null && customer.getCustomerEmailList() != null && customer.getCustomerEmailList().size() > 0) {
				if (customer.getCustomerEmailListDeleted() == null) {
					customer.setCustomerEmailListDeleted(new ArrayList<CustomerEmailVo>());
				}
				customer.getCustomerEmailListDeleted().add(customer.getCustomerEmailList().get(Integer.parseInt(index)));
				customer.getCustomerEmailList().remove(Integer.parseInt(index));
			}
		}
	}

	/**
	 * add address record
	 * 
	 * @param event
	 */
	public void addAddress(ActionEvent event) {
		List<CustomerAddressVo> customerAddressList = customer.getCustomerAddressList();
		if (customerAddressList == null)
			customerAddressList = new ArrayList<CustomerAddressVo>();
		CustomerAddressVo addressVo = new CustomerAddressVo();
		addressVo.initItems();
		customerAddressList.add(addressVo);
		customer.setCustomerAddressList(customerAddressList);
	}

	/**
	 * delete address
	 * 
	 * @param event
	 */
	public void deleteAddress(ActionEvent event) {
		String index = (String) getRequestParameter("index");
		if (index != null && index.length() > 0) {
			if (customer != null && customer.getCustomerAddressList() != null && customer.getCustomerAddressList().size() > 0) {
				if (customer.getCustomerAddressListDeleted() == null) {
					customer.setCustomerAddressListDeleted(new ArrayList<CustomerAddressVo>());
				}
				customer.getCustomerAddressListDeleted().add(customer.getCustomerAddressList().get(Integer.parseInt(index)));

				customer.getCustomerAddressList().remove(Integer.parseInt(index));
			}
		}
	}

	/**
	 * add attribute record
	 * 
	 * @param event
	 */
	public void addAttribute(ActionEvent event) {
		List<CustomerAttributeVo> customerAttributeList = customer.getCustomerAttributeList();
		if (customerAttributeList == null) {
			customerAttributeList = new ArrayList<CustomerAttributeVo>();
		}
		customerAttributeList.add(new CustomerAttributeVo());
		customer.setCustomerAttributeList(customerAttributeList);
	}

	/**
	 * delete attribute record
	 * 
	 * @param event
	 */
	public void deleteAttribute(ActionEvent event) {

		String index = (String) getRequestParameter("attributeIndex");
		if (index != null && index.length() > 0) {
			if (customer != null && customer.getCustomerAttributeList() != null && customer.getCustomerAttributeList().size() > 0) {
				if (customer.getCustomerAttributeListDeleted() == null) {
					customer.setCustomerAttributeListDeleted(new ArrayList<CustomerAttributeVo>());
				}
				customer.getCustomerAttributeListDeleted().add(customer.getCustomerAttributeList().get(Integer.parseInt(index)));
				customer.getCustomerAttributeList().remove(Integer.parseInt(index));
			}
		}

	}

	/**
	 * add discount record
	 * 
	 * @param event
	 */
	public void addDiscount(ActionEvent event) {
		List<CustomerDiscountVo> customerDiscountList = customer.getCustomerDiscountList();
		if (customerDiscountList == null) {
			customerDiscountList = new ArrayList<CustomerDiscountVo>();
		}
		customerDiscountList.add(new CustomerDiscountVo());
		customer.setCustomerDiscountList(customerDiscountList);

	}

	/**
	 * delete discount record
	 * 
	 * @param event
	 */
	public void deleteDiscount(ActionEvent event) {
		String index = (String) getRequestParameter("attributeIndex");
		if (index != null && index.length() > 0) {
			if (customer != null && customer.getCustomerDiscountList() != null && customer.getCustomerDiscountList().size() > 0) {
				if (customer.getCustomerDiscountListDeleted() == null) {
					customer.setCustomerDiscountListDeleted(new ArrayList<CustomerDiscountVo>());
				}
				customer.getCustomerDiscountListDeleted().add(customer.getCustomerDiscountList().get(Integer.parseInt(index)));
				customer.getCustomerDiscountList().remove(Integer.parseInt(index));
			}
		}
	}

	/**
	 * add service event record
	 * 
	 * @param event
	 */
	public void addServiceEvent(ActionEvent event) {
		List<CustomerServiceEventVo> customerServiceEventList = customer.getCustomerServiceEventList();
		if (customerServiceEventList == null)
			customerServiceEventList = new ArrayList<CustomerServiceEventVo>();
		customerServiceEventList.add(new CustomerServiceEventVo());
		customer.setCustomerServiceEventList(customerServiceEventList);
	}

	/**
	 * delete service event record
	 * 
	 * @param event
	 */
	public void deleteServiceEvent(ActionEvent event) {
		String index = (String) getRequestParameter("attributeIndex");
		if (index != null && index.length() > 0) {
			if (customer != null && customer.getCustomerServiceEventList() != null && customer.getCustomerServiceEventList().size() > 0) {
				if (customer.getCustomerServiceEventListDeleted() == null) {
					customer.setCustomerServiceEventListDeleted(new ArrayList<CustomerServiceEventVo>());
				}
				customer.getCustomerServiceEventListDeleted().add(customer.getCustomerServiceEventList().get(Integer.parseInt(index)));
				customer.getCustomerServiceEventList().remove(Integer.parseInt(index));
			}
		}
	}

	/**
	 * month of birthday changed
	 * 
	 * @param event
	 */
	public void dobMonthValueChange(ValueChangeEvent event) {
		String month = (String) event.getNewValue();
		if (month != null && month.trim().length() > 0) {
			dobDayItems = SelectItemsUtil.getDayItems(Integer.parseInt(month));
			if (customer.getDobDay() != null && customer.getDobDay().length() > 0) {
				if (Integer.parseInt(customer.getDobDay()) > dobDayItems.length) {
					customer.setDobDay("");
				}
			}
		}

	}

	public SelectItem[] getDobMonthItems() {
		return dobMonthItems;
	}

	public void setDobMonthItems(SelectItem[] dobMonthItems) {
		this.dobMonthItems = dobMonthItems;
	}

	public SelectItem[] getDobDayItems() {
		return dobDayItems;
	}

	public void setDobDayItems(SelectItem[] dobDayItems) {
		this.dobDayItems = dobDayItems;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public SelectItem[] getServiceItems() {
		return serviceItems;
	}

	public void setServiceItems(SelectItem[] serviceItems) {
		this.serviceItems = serviceItems;
	}

	public SelectItem[] getAttrCodeItems() {
		return attrCodeItems;
	}

	public void setAttrCodeItems(SelectItem[] attrCodeItems) {
		this.attrCodeItems = attrCodeItems;
	}

	public SelectItem[] getSaluteItems() {
		return saluteItems;
	}

	public void setSaluteItems(SelectItem[] saluteItems) {
		this.saluteItems = saluteItems;
	}

	public SelectItem[] getFrChanlItems() {
		return frChanlItems;
	}

	public void setFrChanlItems(SelectItem[] frChanlItems) {
		this.frChanlItems = frChanlItems;
	}

	public SelectItem[] getDiscountItems() {
		return discountItems;
	}

	public void setDiscountItems(SelectItem[] discountItems) {
		this.discountItems = discountItems;
	}

	public SelectItem[] getCustomerStatusItems() {
		return customerStatusItems;
	}

	public void setCustomerStatusItems(SelectItem[] customerStatusItems) {
		this.customerStatusItems = customerStatusItems;
	}

	public String getActiveTab() {
		return activeTab;
	}

	public void setActiveTab(String activeTab) {
		this.activeTab = activeTab;
	}

	public List<MenuItem> getAccessPath() {
		return accessPath;
	}

	public void setAccessPath(List<MenuItem> accessPath) {
		this.accessPath = accessPath;
	}

	public CustomerVo getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerVo customer) {
		this.customer = customer;
	}

	public SelectItem[] getDepartmentItems() {
		return departmentItems;
	}

	public void setDepartmentItems(SelectItem[] departmentItems) {
		this.departmentItems = departmentItems;
	}

}
